package com.hrms.hrms_security.component;

import com.hrms.hrms_security.exception.BusinessException;
import com.hrms.common.utils.ResultMessage;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理器
 */
//扫描这个包下面的错误，当这个包里面的类出现错误时运行下面的方法
@ControllerAdvice(basePackages = "com.blogzc.blog.controller")
public class GlobalExceptionHandler {

    //运行时错误
    @ExceptionHandler(RuntimeException.class)
    public ResultMessage RuntimeerrorResult(RuntimeException runtimeException){
        return new ResultMessage(ResultMessage.Error,runtimeException.getMessage());
    }

    //业务异常处理
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public ResultMessage BusinesserrorResult(BusinessException e){
        return new ResultMessage(ResultMessage.Error,e.getMessage());
    }

    //全局异常处理
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultMessage errorResult(Exception e){
        return new ResultMessage(ResultMessage.Error,e.getMessage());
    }

}